/*------------------------------------------------------------------------------
--------------------------------------------------------------------------------
Copyright (c) 2016, Loongson Technology Corporation Limited.

All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.

3. Neither the name of Loongson Technology Corporation Limited nor the names of
its contributors may be used to endorse or promote products derived from this
software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL LOONGSON TECHNOLOGY CORPORATION LIMITED BE LIABLE
TO ANY PARTY FOR DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
--------------------------------------------------------------------------------
------------------------------------------------------------------------------*/

//*************************************************************************
//   > File Name   : soc_top.v
//   > Description : SoC, included cpu, 2 x 3 bridge,
//                   inst ram, confreg, data ram
//
//           -------------------------
//           |           cpu         |
//           -------------------------
//         inst|                  | data
//             |                  |
//             |        ---------------------
//             |        |    1 x 2 bridge   |
//             |        ---------------------
//             |             |            |
//             |             |            |
//      -------------   -----------   -----------
//      | inst ram  |   | data ram|   | confreg |
//      -------------   -----------   -----------
//
//   > Author      : LOONGSON
//   > Date        : 2017-08-04
//*************************************************************************

//for simulation:
//1. if define SIMU_USE_PLL = 1, will use clk_pll to generate cpu_clk/timer_clk,
//   and simulation will be very slow.
//2. usually, please define SIMU_USE_PLL=0 to speed up simulation by assign
//   cpu_clk/timer_clk = clk.
//   at this time, cpu_clk/timer_clk frequency are both 100MHz, same as clk.
`define SIMU_USE_PLL 0 //set 0 to speed up simulation

module soc_lite_top #(parameter SIMULATION=1'b0)
(
    input         resetn,
    input         clk,

    //------gpio-------
    output [15:0] led,
    output [1 :0] led_rg0,
    output [1 :0] led_rg1,
    output [7 :0] num_csn,
    output [6 :0] num_a_g,
    input  [7 :0] switch,
    output [3 :0] btn_key_col,
    input  [3 :0] btn_key_row,
    input  [1 :0] btn_step,

    // lcd
    inout [15:0] lcd1_db,
    output lcd1_rst,
    output lcd1_cs,
    output lcd1_rs,
    output lcd1_wr,
    output lcd1_rd,
    output lcd1_bl_ctr,
    output lcd1_T_PEN,
    inout lcd1_sda,
    output lcd1_scl,
    inout lcd1_T_CS,

    output [7:0] dot_row,
   output [7:0] dot_col
);
//debug signals
wire [31:0] debug_wb_pc;
wire [3 :0] debug_wb_rf_wen;
wire [4 :0] debug_wb_rf_wnum;
wire [31:0] debug_wb_rf_wdata;


reg [15:0] lcd1_data;

wire [15:0] lcd1_db_confreg;  assign lcd1_db =lcd1_db_confreg;
wire lcd1_rst_confreg;               assign lcd1_rst = lcd1_rst_confreg;
wire lcd1_cs_confreg;                assign lcd1_cs = lcd1_cs_confreg;
wire lcd1_rs_confreg;                 assign lcd1_rs = lcd1_rs_confreg;
wire lcd1_wr_confreg;                assign lcd1_wr = lcd1_wr_confreg;
wire  lcd1_rd_confreg;                assign lcd1_rd = lcd1_rd_confreg;
wire lcd1_bl_ctr_confreg;         assign lcd1_bl_ctr = lcd1_bl_ctr_confreg;
wire lcd1_T_PEN_confreg;       assign lcd1_T_PEN = lcd1_T_PEN_confreg;
wire lcd1_sda_confreg;             assign lcd1_sda =  lcd1_sda_confreg;
wire lcd1_scl_confreg;               assign lcd1_scl = lcd1_scl_confreg;
wire lcd1_T_CS_confreg;          assign lcd1_T_CS = lcd1_T_CS_confreg;

//clk and resetn
wire cpu_clk;
wire timer_clk;
reg cpu_resetn;
always @(posedge cpu_clk)
begin
    cpu_resetn <= resetn;
end
generate if(SIMULATION && `SIMU_USE_PLL==0)
begin: speedup_simulation
    assign cpu_clk   = clk;
    assign timer_clk = clk;
end
else
begin: pll
    clk_pll clk_pll
    (
        .clk_in1 (clk),
        .cpu_clk (cpu_clk),
        .timer_clk (timer_clk)
    );
end
endgenerate

//cpu inst sram
wire        cpu_inst_en;
wire [3 :0] cpu_inst_wen;
wire [31:0] cpu_inst_addr;
wire [31:0] cpu_inst_wdata;
wire [31:0] cpu_inst_rdata;
//cpu data sram
wire        cpu_data_en;
wire [3 :0] cpu_data_wen;
wire [31:0] cpu_data_addr;
wire [31:0] cpu_data_wdata;
wire [31:0] cpu_data_rdata;

//data sram
wire        data_sram_en;
wire [3 :0] data_sram_wen;
wire [31:0] data_sram_addr;
wire [31:0] data_sram_wdata;
wire [31:0] data_sram_rdata;
//conf
wire        conf_en;
wire [3 :0] conf_wen;
wire [31:0] conf_addr;
wire [31:0] conf_wdata;
wire [31:0] conf_rdata;

//cpu
mycpu_top cpu(
    .clk              (cpu_clk   ),
    .resetn           (cpu_resetn),  //low active
    .int              (6'd0      ),  //interrupt,high active

    .inst_sram_en     (cpu_inst_en   ),
    .inst_sram_wen    (cpu_inst_wen  ),
    .inst_sram_addr   (cpu_inst_addr ),
    .inst_sram_wdata  (cpu_inst_wdata),
    .inst_sram_rdata  (cpu_inst_rdata),

    .data_sram_en     (cpu_data_en   ),
    .data_sram_wen    (cpu_data_wen  ),
    .data_sram_addr   (cpu_data_addr ),
    .data_sram_wdata  (cpu_data_wdata),
    .data_sram_rdata  (cpu_data_rdata),

    //debug
    .debug_wb_pc      (debug_wb_pc      ),
    .debug_wb_rf_wen  (debug_wb_rf_wen  ),
    .debug_wb_rf_wnum (debug_wb_rf_wnum ),
    .debug_wb_rf_wdata(debug_wb_rf_wdata)
);

//inst ram
inst_ram inst_ram
(
    .clka  (cpu_clk            ),
    .ena   (cpu_inst_en        ),
    .wea   (cpu_inst_wen       ),   //3:0
    .addra (cpu_inst_addr[19:2]),   //17:0
    .dina  (cpu_inst_wdata     ),   //31:0
    .douta (cpu_inst_rdata     )    //31:0
);

bridge_1x2 bridge_1x2(
    .clk             ( cpu_clk         ), // i, 1
    .resetn          ( cpu_resetn      ), // i, 1

    .cpu_data_en     ( cpu_data_en     ), // i, 4
    .cpu_data_wen    ( cpu_data_wen    ), // i, 4
    .cpu_data_addr   ( cpu_data_addr   ), // i, 32
    .cpu_data_wdata  ( cpu_data_wdata  ), // i, 32
    .cpu_data_rdata  ( cpu_data_rdata  ), // o, 32

    .data_sram_en    ( data_sram_en    ), // o, 4
    .data_sram_wen   ( data_sram_wen   ), // o, 4
    .data_sram_addr  ( data_sram_addr  ), // o, `DATA_RAM_ADDR_LEN
    .data_sram_wdata ( data_sram_wdata ), // o, 32
    .data_sram_rdata ( data_sram_rdata ), // i, 32

    .conf_en         ( conf_en         ), // o, 1
    .conf_wen        ( conf_wen        ), // o, 4
    .conf_addr       ( conf_addr       ), // o, 32
    .conf_wdata      ( conf_wdata      ), // o, 32
    .conf_rdata      ( conf_rdata      )  // i, 32
 );

//data ram
data_ram data_ram
(
    .clka  (cpu_clk             ),
    .ena   (data_sram_en        ),
    .wea   (data_sram_wen       ),   //3:0
    .addra (data_sram_addr[17:2]),   //15:0
    .dina  (data_sram_wdata     ),   //31:0
    .douta (data_sram_rdata     )    //31:0
);

//confreg
confreg #(.SIMULATION(SIMULATION)) confreg
(
    .clk         ( cpu_clk    ),  // i, 1
    .timer_clk   ( timer_clk  ),  // i, 1
    .resetn      ( cpu_resetn ),  // i, 1
    .conf_en     ( conf_en    ),  // i, 1
    .conf_wen    ( conf_wen   ),  // i, 4
    .conf_addr   ( conf_addr  ),  // i, 32
    .conf_wdata  ( conf_wdata ),  // i, 32
    .conf_rdata  ( conf_rdata ),  // o, 32
    .led         ( led        ),  // o, 16
    .led_rg0     ( led_rg0    ),  // o, 2
    .led_rg1     ( led_rg1    ),  // o, 2
    .num_csn     ( num_csn    ),  // o, 8
    .num_a_g     ( num_a_g    ),  // o, 7
    .switch      ( switch     ),  // i, 8
    .btn_key_col ( btn_key_col),  // o, 4
    .btn_key_row ( btn_key_row),  // i, 4
    .btn_step    ( btn_step   ),   // i, 2

    .lcd_db (lcd1_db_confreg),
    .lcd_rst (lcd1_rst_confreg),
    .lcd_rd(lcd1_rd_confreg),
    .lcd_cs (lcd1_cs_confreg),
    .lcd_rs (lcd1_rs_confreg),
    .lcd_wr (lcd1_wr_confreg),
    .lcd_bl_ctr (lcd1_bl_ctr_confreg),
    .lcd_T_PEN (lcd1_T_PEN_confreg),
    .lcd_sda (lcd1_sda_confreg),
    .lcd_scl  (lcd1_scl_confreg),
    .lcd_T_int (lcd1_T_CS_confreg),

    .led_dot     (led_dot     )
);



endmodule
